<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/21
 * Time: 9:19
 */

use yii\web\View;
use yii\helpers\Url;

$this->registerJsFile('@web/js/dropzone.js');
$this->registerCssFile('@web/css/dropzone.css');
$this->title = Yii::t('app', 'Import');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Employees'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<style type="text/css">
    .dropzone {
        border: 2px dashed #0087F7;
        border-radius: 5px;
        background: white;
    }
</style>
<div class="panel panel-primary">
    <div class="panel-heading">
        <strong><?= Yii::t('app', 'Upload Files') ?></strong>
    </div>
    <div class="panel-body">
        <div class="text-bold ">
            <span class="text-danger" style="font-size: 1.2em;">点击此处下载
                <a href="<?= Yii::getAlias("@web/template") ?>/员工信息导入模板.xlsx">员工信息导入模板</a>
            </span>
        </div>
        <div id="dropzone" class="dropzone dropzone-previews">
            <div class="dz-message">点击选择或拖放文件到这里.<br/>
                <span class="note needsclick">( 支持的文件类型：<strong>Excel,CSV</strong> )</span>
            </div>
            <input type="hidden" name="unidentify-id" value=""/>
        </div>
    </div>
</div>

<script>
    <?php $this->beginBlock('js_block')?>

    Dropzone.autoDiscover = false;
    $(function () {
        var dropz = new Dropzone("#dropzone", {
            url: "<?=Url::toRoute(['/file/upload'])?>",
            paramName: 'file',
            maxFiles: 10,
            maxFilesize: 512,
            maxThumbnailFilesize: 10,
            addRemoveLinks: true,
            parallelUploads: 4,
//            uploadMultiple:true,
            dictRemoveFile: '删除',
            dictCancelUpload: '取消上传',
            acceptedFiles: ".xls,.xlsx,.csv",
            dictMaxFilesExceeded: "您最多只能上传10个文件！",
            dictResponseError: '文件上传失败!',
            dictInvalidFileType: "你不能上传该类型文件,文件类型只能是*.xls,*.xlsx,*.csv",
            dictFallbackMessage: "浏览器不受支持",
            dictFileTooBig: "文件过大上传文件最大支持.",
            params: {'_csrf-backend': '<?= Yii::$app->request->csrfToken ?>'},
            init: function() {
                this.on("addedfile", function(file) {
                    //alert("file.fid: " +　file.fid);
                });
                this.on("sending", function (file, xhr, formData ) {
//                    formData.append('id', $('input[name="unidentify-id"]').val())
                });
                this.on("success", function (file, responseText) {
                    var response = eval("(" + responseText + ")");
                    //用后台文件名替换前台文件名,以便于删除用
                    file.fid = response.data;
//                    SuccessDlg.alert(file.fid);
                    SuccessDlg.alert(response.message);
                });
                this.on("removedfile", function(file) {
                    $.ajax({
                        type: "GET",
                        url: "/file/delete-by-filename",
                        data:{
                            "filename" : file.fid
                        },
                        dataType: "json",
                        success: function(response){
                            SuccessDlg.alert(response.message);
                        }
                    })
                });
            }
        });
    });


    <?php
    $this->endBlock();
    $this->registerJs($this->blocks['js_block'], View::POS_END);
    ?>
</script>




